package com.itheima.contorller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.common.TokenHolder;
import com.itheima.domian.MyPage;
import com.itheima.domian.Orders;
import com.itheima.domian.vo.OrderVo;
import com.itheima.mapper.client.OrderMapper;
import com.itheima.service.client.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author 12441
 * @Date 2022/8/18 14:52
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderMapper orderMapper;


    @GetMapping("/page")
    public R<MyPage> page(HttpServletRequest httpServerRequest) {

        MyPage myPage = new MyPage();

        String name = httpServerRequest.getParameter("name");
        Integer page = Integer.valueOf(httpServerRequest.getParameter("page"));
        Integer size = Integer.valueOf(httpServerRequest.getParameter("pageSize"));


        IPage ipage=new Page(page,size);

        orderMapper.selectPage(ipage,null);

        //查询数据ipage.getRecords();
        long total = ipage.getTotal();
        if (total==0){
            return R.error("无数据");
        }
        myPage.setRecords(ipage.getRecords());
        myPage.setTotal(ipage.getTotal());
        myPage.setSize(size);
        myPage.setCurrent(page);
        return R.success(myPage,"查询完成");
    }
    @PutMapping
    public R<String> page(@RequestBody Orders orders) {
        Long id = orders.getId();
        Orders oderById = orderService.getById(id);
        oderById.setStatus(orders.getStatus());
        orderService.updateById(oderById);
        return R.success("修改状态成功");
    }
}